-- curse_weaken
-- created by zhuangsl
-- 诅咒术减少攻击

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 如果免疫了该技能
        if SkillM.immunitySkill(source, target, skillId) then
            SkillM.getSequence(target):immunity(source, target, skillId);
            return {};
        end

        extra = extra or {};
        local lastRound = extra.lastRound;

        local effectRound = -1;
        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[3] > 0 then
            effectRound = round + para[3] + 1;
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        -- 魔力
        local magic = source:getMagic();
        -- 被对方百分比削弱
        prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 威力
        local force = para[2];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        --最终威力
        local force = math.modf(force * (addon_rate + 1000)/1000);

        -- 如果是boss
        if target:isBoss() then
            force = force / 2;
        end

        local v = 1 + math.modf(force * (magic + 3) / 1000);

        local condition = {
            ["value"] = v,
            ["end_round"] = effectRound,
        };

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 天赋起效，降低血量
        prop = PropM.combine(source, "curse_reduce_hp", skillId);
        local reduceHp = PropM.apply(prop, v);
        if reduceHp > 0 then
            local currHp = target:getHp();
            v = math.max(1, currHp - reduceHp);
            target:setRealHp(v);
            -- 记录降低的生命值，清除时使用
            condition["hp"] = currHp - v;
        end

        -- 附加状态
        CombatStatusM.applyStatus(target, "curse_magic", condition);

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), tonumber(arr[3] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 魔力
        local magic = source:getMagic();

        local base = para[2];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        --最终威力
        local force = math.modf(base * (addon_rate + 1000) / 1000);

        local vbase = math.abs(1 + math.modf(base * (magic + 3) / 1000));
        local vforce = math.abs(1 + math.modf(force * (magic + 3) / 1000));
        local vaddon = vforce - vbase;
        if vaddon == 0 then
            desc = string.gsub(desc, "{curse_weaken}", vforce);
        else
            desc = string.gsub(desc, "{curse_weaken}", vbase .. "(+" .. vaddon .. ")");
        end

        local key = FormulaM.invoke("CALC_SKILL_APPEND_PROP", skillId);
        local prop = PropM.combine(source, key, skillId);
        local append = PropM.apply(prop, vforce);

        desc = string.gsub(desc, "{append}", append);

        -- 回合数的显示
        local baseRound = para[3];
        local round = para[3];
        -- 增加持续时间的效果
        prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        -- 属性
        desc = string.gsub(desc, "{attrib}", FieldsM.getFieldName(para[1]));

        return desc;
    end,
};
